    //
//  LoginViewController.m
//  Jacksms-Iphone
//
//  Created by Daniele Ciriello on 18/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"


@implementation LoginViewController





// importiamo i componenti 
@synthesize usernametextfield;
@synthesize passwordtextfield;
@synthesize signinbutton;
@synthesize logginginactivityindicator;
@synthesize loglabel;

// azione richiamata alla pressione del bottone iscriviti




- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == usernametextfield) {
        [passwordtextfield becomeFirstResponder];
    } 
    return NO;
}
	

-(IBAction)login{
	[logginginactivityindicator startAnimating];
	NSString *username = usernametextfield.text;
	NSString *password = passwordtextfield.text;
	//conversione in base 64 (da fare con una funzione esterna ma non riesco!! HELP
	NSData *usernameSourceData = [username dataUsingEncoding:NSUTF8StringEncoding];
	NSString *base64Username = [usernameSourceData base64Encoding];
	NSData *passwordSourceData = [password dataUsingEncoding:NSUTF8StringEncoding];
	NSString *base64Password = [passwordSourceData base64Encoding];
	//replaceOccurrencesOfString:withString:
	base64Username = [base64Username stringByReplacingOccurrencesOfString:@"+" withString:@"-"]; 
	base64Username = [base64Username stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
	base64Password = [base64Password stringByReplacingOccurrencesOfString:@"+" withString:@"-"]; 
	base64Password = [base64Password stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
	//this is the string to insert in the http url for receive synchronization files
	NSString *guest = [NSString stringWithFormat:@"%@/%@", base64Username, base64Password];
	loglabel.text=guest;
	
	//chiamo la funzione firstcheckuser che si occupa di salvare i dati nel db
	loglabel.text=[DBAccess firstCheckUser:guest];
	


}

/*

- (NSString*)base64Encoder:(NSString *)str {
	NSMutableString *mutStr=[NSMutableString stringWithCapacity:[str length]];
	[mutStr setString: str];
	NSData *mutStrSourceData = [mutStr dataUsingEncoding:NSUTF8StringEncoding];
	NSMString *base64MutStr = [mutStrSourceData base64Encoding];
	return base64MutStr;
	//replaceOccurrencesOfString:withString:options:range:
	[mutstr replaceOccurrencesOfString:@"+39" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"(" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@")" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"," withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"*" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"#" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"-" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	[ret replaceOccurrencesOfString:@"+" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [ret length])];
	return ret;
}*/


-(IBAction)signin{
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	
	[logginginactivityindicator dealloc];
	[passwordtextfield dealloc];
	[usernametextfield dealloc];
	[signinbutton dealloc];
	[loglabel dealloc];
    [super dealloc];
}


@end
